package com.bjim.resource;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.bjim.AResult;
import com.bjim.common.RYUtils;
import com.bjim.model.Group;
import com.bjim.model.UserGroup;

import cn.dreampie.common.util.Maper;
import cn.dreampie.orm.Record;
import cn.dreampie.orm.transaction.Transaction;
import cn.dreampie.route.annotation.API;
import cn.dreampie.route.annotation.GET;
import cn.dreampie.route.annotation.POST;
import io.rong.models.CodeSuccessReslut;
import io.rong.models.GroupUser;
import io.rong.models.GroupUserQueryReslut;
/*
 * 群组API类
 */
@API("group")
public class GroupResource extends ApiResource{
	/**
	 * 根据群组ID、userid查找群组和关系
	 * @param groupid
	 * @param userid
	 * @return
	 */
	@GET("/:groupid")
	@POST("/:groupid")
	public AResult getGroupInfo(int groupid,String userid){
		Group group;
		if(null==userid){
			group=Group.dao.findById(groupid);
		}else{
			group=Group.dao.findGroup(groupid, userid);
		}
		return new AResult(group);
	}
	
	/**
	 * 创建群组
	 * @param userids
	 * @param groupname
	 * @param groupmemo
	 * @return
	 * @throws Exception
	 */
	@GET("/create")
	@POST("/create")
	@Transaction
	public AResult createGroup(String userids,String groupname,String groupmemo) throws Exception{
		Group group=new Group();
		group.set("groupname", groupname);
		group.set("groupmemo", groupmemo);
		group.save();
		Object groupid=group.get("groupid");
		System.out.println("-----------");
		System.out.println(userids);
		String[] users=userids.split(",");
		System.out.println(users);
		for(int i=0;i<users.length;i++){
			String userid=users[i];
			UserGroup userGroup=new UserGroup();
			userGroup.set("userid", userid);
			userGroup.set("groupid", groupid);
			if(i==0)
				{userGroup.set("isadmin", 1);
				group.set("adminuser", userid);
				group.update();
				}
			userGroup.save();
		}
			CodeSuccessReslut result=RYUtils.RyGroup.create(users, String.valueOf(groupid), groupname);
			if(result.getCode()==200){
				return new AResult(group);
			}
		return new AResult("创建失败");
	}
	/**
	 * 加入群组
	 * @param groupid
	 * @param userids
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/join")
	@POST("/:groupid/join")
	@Transaction
	public AResult joinGroup(int groupid,String userids) throws Exception{
		Group group=Group.dao.findById(groupid);
		String[] users=userids.split(",");
		for(String userid:users){
			UserGroup u=UserGroup.dao.findFirstBy("groupid=? and userid=?", groupid,userid);
			if(u==null){
				UserGroup userGroup=new UserGroup();
				userGroup.set("userid", userid);
				userGroup.set("groupid", groupid);
				userGroup.save();
				}
		}
		CodeSuccessReslut result;
			result = RYUtils.RyGroup.join(users, String.valueOf(groupid), group.getStr("groupname"));
			if(result.getCode()==200){
				return new AResult(group);
			}
		
		return new AResult("加入失败");
	}
	/**
	 * 退出群组
	 * @param groupid
	 * @param userids
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/quit")
	@POST("/:groupid/quit")
	@Transaction
	public AResult quitGroup(int groupid,String userids) throws Exception{
		String[] users=userids.split(",");
		for(String userid:users){
			String sql="delete from ts_usergroup where userid='"+userid+"' and groupid="+groupid;
			new Record().execute(sql);
		}
			CodeSuccessReslut  result=RYUtils.RyGroup.quit(users, String.valueOf(groupid));
			if(result.getCode()==200){
				return new AResult();
			}
		return new AResult("退出失败");
	}
	
	/**
	 * 解散群组
	 * @param groupid
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/destroy")
	@POST("/:groupid/destroy")
	@Transaction
	public AResult destroyGroup(int groupid) throws Exception{
		String sql="delete from ts_group where groupid="+groupid;
		String sql1="delete from ts_usergroup where groupid="+groupid;
		new Record().execute(sql);
		new Record().execute(sql1);
		CodeSuccessReslut  result=RYUtils.RyGroup.dismiss("0", String.valueOf(groupid));
		if(result.getCode()==200){
			return new AResult();
		}
		return new AResult("解散失败");
	}
	
	
	/**
	 * 设置管理员
	 * @param groupid
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/setadmin/:userid")
	@POST("/:groupid/setadmin/:userid")
	@Transaction
	public AResult setAdmin(int groupid,String userid) throws Exception{
		
		Group group=Group.dao.findById(groupid).set("adminuser", userid);
		group.update();
		return new AResult(group);
	}
	
	/**
	 * 设置群组是否开放
	 * @param groupid
	 * @param isopen
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/setopen/:isopen")
	@POST("/:groupid/setopen/:isopen")
	@Transaction
	public AResult setOpen(int groupid,int isopen) throws Exception{
		Group group=Group.dao.findById(groupid).set("isopen", isopen);
		group.update();
		return new AResult(group);
	}
	/**
	 * 设置群组信息
	 * @param groupid
	 * @param groupname
	 * @param groupmemo
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/setgroupinfo")
	@POST("/:groupid/setgroupinfo")
	@Transaction
	public AResult setGroupInfo(int groupid,String groupname,String groupmemo) throws Exception{
		Group group=Group.dao.findById(groupid);
		if(group==null)
			return new AResult("无此群组");
		group.set("groupname", groupname).set("groupmemo", groupmemo).update();
		return new AResult(group);
	}
	/**
	 * 设置群组消息是否开启通知
	 * @param groupid
	 * @param isnotify
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/setnotify")
	@POST("/:groupid/setnotify")
	@Transaction
	public AResult setNotify(int groupid,int isnotify,String userid) throws Exception{
		UserGroup group=UserGroup.dao.findFirstBy("groupid=? and userid=?", groupid,userid);
		if(group==null){
			return new AResult("无此群组");
		}
		group.set("isnotify",isnotify).update();
		return new AResult();
	}
	
	/**
	 * 获取群组人员
	 * @param groupid
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/users")
	@POST("/:groupid/users")
	@Transaction
	public AResult getUsers(String groupid) throws Exception{
		String users="";
		GroupUserQueryReslut  result=RYUtils.RyGroup.queryUser(groupid);
		if(result.getCode()==200){
			List<GroupUser> gus=result.getUsers();
			for(int i=0;i<gus.size();i++){
				if(i!=0){
					users+=",";
				}
				GroupUser ug=gus.get(i);
				users+=ug.getId();
				
			}
			return new AResult(Maper.of("users",users));
		}
		return new AResult("获取失败");
	}
	/**
	 * 获取群组人员信息
	 * @param groupid
	 * @return
	 * @throws Exception
	 */
	@GET("/:groupid/usersinfo")
	@POST("/:groupid/usersinfo")
	@Transaction
	public AResult getUsersInfo(String groupid) throws Exception{
		List<Record> records=new Record().find("select a.userid,a.username,a.portrait_uri,b.isnotify,a.hrEmployee from ts_user a,ts_usergroup b where a.userid=b.userid and b.groupid=? ", groupid);
		for(Record user:records){
			user.put("hrEmployee", JSONObject.toJSON(user.get("hrEmployee")));
		}
		return new AResult(records);
	}
	
	/**
	 * 根据群组名称查找群组
	 * @param groupname
	 * @return
	 */
	@GET("/find/:groupname")
	@POST("/find/:groupname")
	public AResult findGroup(String groupname){
		List<Group> groups=Group.dao.findBy("groupname like '%"+groupname+"%'");
		return new AResult(groups);
	}
	
	/**
	 * 查找所有群组
	 * @return
	 */
	@GET("/all")
	@POST("/all")
	public AResult getAllGroup(){
		return new AResult(Group.dao.findAll());
	}
}
